package com.nc.delivery_project.client.departments;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.ServerFailure;
import com.nc.delivery_project.client.ClientFactory;


import com.nc.delivery_project.client.request.DepartmentRequest;
import com.nc.delivery_project.shared.proxy.DepartmentProxy;

import java.util.List;


public class DepartmentsActivity extends AbstractActivity implements DepartmentsPresenter{

    private DepartmentsView view;
    private ClientFactory clientFactory;
    private String[] tokens;

    public DepartmentsActivity(DepartmentsPlace place, ClientFactory clientFactory) {
        this.clientFactory = clientFactory;
        tokens = place.getToken().split(":");
    }

    @Override
    public void start(AcceptsOneWidget panel, EventBus eventBus) {
        if (view == null) {
            view = new DepartmentsView();
        }

        view.setPresenter(this);
        panel.setWidget(view.asWidget());
        view.start();
       
        if (tokens[0].equals("city")){
            if (tokens[1]!=null){
            int id = Integer.parseInt(tokens[1]);
                loadDepartmentsByCityId(id);
            }
        }

        else 
        loadDepartments();
    }
    
  
	@Override
    public void goTo(Place place) {
        clientFactory.getPlaceController().goTo(place);
    }

    @Override
    public ClientFactory getClientFactory() {
        return clientFactory;
    }

    //loadDepartmentsWithOrders()????
    
    @Override
    public void loadDepartments() {
        final DepartmentRequest deptRequest = clientFactory.getRequestFactory().createDepartmentRequest();

        deptRequest.read().with("city","city.statePr","city.statePr.country").fire(new Receiver<List<DepartmentProxy>>() {
            @Override
            public void onSuccess(List<DepartmentProxy> response) {
                GWT.log("success");
                view.setDataList(response);
            }

            @Override
            public void onFailure(ServerFailure error) {
                super.onFailure(error);
                GWT.log(error.toString());
            }
        });
    }

	@Override
	public void loadDepartmentsByCityId(int cityId) {
		
        final DepartmentRequest deptRequest = clientFactory.getRequestFactory().createDepartmentRequest();

        deptRequest.loadDeptByCityId(cityId).with("city","city.statePr","city.statePr.country").fire(new Receiver<List<DepartmentProxy>>() {
            @Override
            public void onSuccess(List<DepartmentProxy> response) {
                GWT.log("success");
                GWT.log(response.get(0).getCity().getCityTitle());

                view.setDataList(response);
            }

            @Override
            public void onFailure(ServerFailure error) {
                super.onFailure(error);
                GWT.log(error.toString());
            }
        });
	}


}
